﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Jiz.App.Domain.Services
{
    public class CreateService
    {
        public bool CreateAdClickRecord(string os, string platform, string app, string version, string mac)
        {
            using(AppDbContext dbContext = new AppDbContext())
            {
                Models.AdClickRecord model = new Models.AdClickRecord();
                model.Id = Guid.NewGuid();
                model.OS = os;
                model.Platform = platform;
                model.App = app;
                model.Version = version;
                model.Mac = mac;
                model.CreatedTime = DateTime.Now;
                dbContext.Set<Models.AdClickRecord>().Add(model);

                try
                {
                    dbContext.SaveChanges();
                    return true;
                }
                catch(Exception ex)
                {
                    return false;
                }
            }
        }

        public bool CreateMACNickname(string os, string mac, string name)
        {
            using (AppDbContext dbContext = new AppDbContext())
            {
                Models.MACNickname model = dbContext.Set<Models.MACNickname>().SingleOrDefault(p => p.OS == os && p.MAC == mac);
                if(model == null)
                {
                    model = new Models.MACNickname();
                    model.Id = Guid.NewGuid();
                    model.OS = os;
                    model.MAC = mac;
                    model.Name = name;
                    dbContext.Set<Models.MACNickname>().Add(model);
                }
                else
                {
                    model.Name = name;
                    dbContext.Entry<Models.MACNickname>(model).State = System.Data.EntityState.Modified;
                }

                try
                {
                    dbContext.SaveChanges();
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
        }
    }
}
